package sis.report;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

import sis.studentinfo.Session;
import sis.studentinfo.Student;


public class RosterReport {
	public  static final String NEWLINE = System.getProperty("line.separator");
	public static final String ROSTER_REPORT_HEADER = "Student%n-%n";
	public static final String ROSTER_REPORT_FOOTER = "%n# students = %d%n";
	private Session session;
	private Writer writer;
	RosterReport(Session session){
		this.session = session;
	}
	
	private void writeHearder(StringBuilder buffer) {
		buffer.append(ROSTER_REPORT_HEADER);
		
	}
	public void writeReport(Writer writer) throws IOException {
		this.writer = writer;
		writeHearder();
		writeBody();
		writeFooter();
		
	}
	public void writeReport(String filename) throws IOException{
		Writer bufferedWriter = new BufferedWriter(new FileWriter(filename));
		try {
			writeReport(bufferedWriter);
		} finally {
			bufferedWriter.close();
		}
	}
	private void writeFooter() throws IOException {
		writer.write(String.format(ROSTER_REPORT_FOOTER, session.getAllStudents().size()));
	}
	private void writeBody() throws IOException {
		for(Student student: session.getAllStudents())
			writer.write(String.format(student.getName() + "%n"));
	}
	private void writeHearder() throws IOException {
		writer.write(String.format(ROSTER_REPORT_HEADER));
	}

}
